package synchronize;

import threadpool.ThreadPoolFactory;

import java.util.concurrent.ExecutorService;

public class SynchronizedNotifyDemo {

    public synchronized void testWait() {
        System.out.println(Thread.currentThread().getName() + " Start-----");
        try {
            wait();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println(Thread.currentThread().getName() + " End-----");
    }

    public static void main(String[] args) throws InterruptedException {
        final SynchronizedNotifyDemo notifyDemo = new SynchronizedNotifyDemo();
        ExecutorService threadPool = ThreadPoolFactory.getThreadPool();
        for (int i = 0; i < 5; i++) {
            threadPool.execute(new Runnable() {
                @Override
                public void run() {
                    notifyDemo.testWait();
                }
            });
        }

        Thread.sleep(1000);

        //只通过锁对象唤醒一个线程
        synchronized (notifyDemo) {
            System.out.println("notify");
            notifyDemo.notify();
        }

        Thread.sleep(1000);

        //通过锁对象唤醒所有线程
        synchronized (notifyDemo) {
            System.out.println("notify all");
            notifyDemo.notifyAll();
        }

        threadPool.shutdown();
    }

}
